package com.example.purchase.view.activity;

import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.util.Log;
import android.view.View;

import com.example.purchase.R;
import com.example.purchase.adapter.HotRecyclerViewAdapter;
import com.example.purchase.adapter.MainRecyclerViewAdapter;
import com.example.purchase.bean.BaseBean;
import com.example.purchase.bean.HotBean;
import com.example.purchase.bean.MainBean;
import com.example.purchase.databinding.ActivitySearchBinding;
import com.example.purchase.network.RetrofitClient;
import com.example.purchase.network.service.HttpService;
import com.example.purchase.view.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.functions.Consumer;
import io.reactivex.rxjava3.functions.Predicate;
import io.reactivex.rxjava3.schedulers.Schedulers;

public class SearchActivity extends BaseActivity<ActivitySearchBinding> implements View.OnClickListener {

    private List<HotBean> list = new ArrayList<>();
    private HttpService service = RetrofitClient.getInstance().getService(HttpService.class);

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_search;
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initEmpty() {

    }

    @Override
    protected void initClick() {
        dataBinding.searchConfirm.setOnClickListener(this);
    }

    @Override
    protected void initTimeOut() {
        service.getHotTicketList().subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .filter(new Predicate<BaseBean<List<HotBean>>>() {
                    @Override
                    public boolean test(BaseBean<List<HotBean>> listBaseBean) throws Throwable {
                        return String.valueOf(listBaseBean.getCode()).equals("200");
                    }
                })
                .subscribe(new Consumer<BaseBean<List<HotBean>>>() {
            @Override
            public void accept(BaseBean<List<HotBean>> listBaseBean) throws Throwable {
                dataBinding.recyclerView.setAdapter(new HotRecyclerViewAdapter(getApplicationContext(),listBaseBean));
                dataBinding.recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL){
                    @Override
                    public boolean canScrollVertically() {
                        return false;
                    }
                });
                Log.e("TAG", "accept: " + listBaseBean.getCode());
            }
        });
    }

    @Override
    protected boolean isStatusBarEnabled() {
        return false;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.search_confirm:
                service.getSearchTicket("1",String.valueOf(dataBinding.searchText.getText()),token)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .filter(new Predicate<BaseBean<List<MainBean>>>() {
                            @Override
                            public boolean test(BaseBean<List<MainBean>> listBaseBean) throws Throwable {
                                return String.valueOf(listBaseBean.getCode()).equals("200");
                            }
                        }).subscribe(new Consumer<BaseBean<List<MainBean>>>() {
                    @Override
                    public void accept(BaseBean<List<MainBean>> listBaseBean) throws Throwable {
                        dataBinding.recommend.setVisibility(View.GONE);
                        dataBinding.searchRecycler.setVisibility(View.VISIBLE);
                        dataBinding.backTop.setVisibility(View.VISIBLE);
                        dataBinding.searchRecycler.setAdapter(new MainRecyclerViewAdapter(listBaseBean,getApplicationContext()));
                        dataBinding.searchRecycler.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));
                        Log.e("TAG", "accept: " + listBaseBean.getData().toString());
                    }
                });
                break;
        }
    }
}